package ObjectVC;

import java.util.ArrayList;
import java.util.Date;
import javax.swing.JOptionPane;

public aspect Delete {
	after(Object actualObj, Action action, ArrayList<Object> actionParams): 
		VCOperations.VCops(actualObj, action, actionParams) 
		&& if(action.equals(Action.Delete)){
		if (null != actionParams && actionParams.size()>0){
			Object obj = actionParams.get(0);
			Integer idx = null;
			Date timestamp = null;
			DeleteMode delMode = null;
			
			if (null == obj){
				JOptionPane.showMessageDialog(null, "Object is null");
				return;
			}
			
			if(actionParams.size() != 2){
				JOptionPane.showMessageDialog(null, "Not enough parameters for update");
				return;
			}
			
			if(actionParams.get(1).getClass().equals(DeleteMode.class)){
				delMode = (DeleteMode) actionParams.get(1);
				if (DeleteMode.DeleteObj.equals(delMode))
					DB.deleteObject(obj);
				else
					DB.deleteObjVersions(obj);
			}
			else if(actionParams.get(1).getClass().equals(Integer.class)){
				idx = (Integer) actionParams.get(1);
				DB.deleteVersion(obj, idx);
			}
			else if(actionParams.get(1).getClass().equals(Date.class)){
				timestamp = (Date) actionParams.get(1);
				DB.deleteVersion(obj, timestamp);
			}
		}
	}
}
